////////////////////////////////////////////////////////////////////////////////
// Filename: polygon.cpp
////////////////////////////////////////////////////////////////////////////////
#include "polygon.hpp"

miv::Polygon::Polygon() {
	m_vertexBuffer = NULL;
	m_indexBuffer = NULL;
	m_vertices = NULL;
	m_indices = NULL;
}

miv::Polygon::~Polygon() {

}

bool miv::Polygon::Initialize(Vector2D** vertices, unsigned long* indices, int vertexCount, int indexCount, D3DXVECTOR4 color,ID3D11Device* device) {
	miv::IRenderble::Initialize();

	bool result;

	m_vertexCount = vertexCount;
	m_indexCount = indexCount;
	m_vertices = vertices;
	m_indices = indices;

	result = InitializeBuffers(color, device);
	if (!result) {
		return false;
	}

	return true;
}

void miv::Polygon::Shutdown() {
	ShutdownBuffers();
}

void miv::Polygon::Draw(Graphics* graphics) {
	DrawBuffers(graphics);
}

void miv::Polygon::SetActive(ID3D11DeviceContext* deviceContext) {
	unsigned int stride;
	unsigned int offset;

	stride = sizeof(VertexType::PositionColor);
	offset = 0;

	deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
	deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
	deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
}

int miv::Polygon::GetVertexCount() {
	return m_vertexCount;
}

int miv::Polygon::GetIndexCount() {
	return m_indexCount;
}

bool miv::Polygon::InitializeBuffers(D3DXVECTOR4 color, ID3D11Device* device) {
		VertexType::PositionColor* vertices;
		D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
		D3D11_SUBRESOURCE_DATA vertexData, indexData;
		HRESULT result;

		vertices = new VertexType::PositionColor[m_vertexCount];
		if (!vertices) {
			return false;
		}

// Obsolet
//		vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
//		vertices[0].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
//
//		vertices[1].position = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
//		vertices[1].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
//
//		vertices[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
//		vertices[2].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
//
//		indices[0] = 0;
//		indices[1] = 1;
//		indices[2] = 2;

		for (int i = 0; i < m_vertexCount; ++i) {
			vertices[i].position = D3DXVECTOR3(m_vertices[i]->GetX(), m_vertices[i]->GetY(), 0.0f);
			vertices[i].color = color;

//			indices[i] = i;
		}

//		indices[0] = 0;
//		indices[1] = 1;
//		indices[2] = 2;
//		indices[3] = 0;
//		indices[4] = 2;
//		indices[5] = 3;
//		indices[6] = 0;
//		indices[7] = 3;
//		indices[8] = 4;

		vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
		vertexBufferDesc.ByteWidth = sizeof(VertexType::PositionColor) * m_vertexCount;
		vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		vertexBufferDesc.CPUAccessFlags = 0;
		vertexBufferDesc.MiscFlags = 0;
		vertexBufferDesc.StructureByteStride = 0;

		vertexData.pSysMem = vertices;
		vertexData.SysMemPitch = 0;
		vertexData.SysMemSlicePitch = 0;

		result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
		if (FAILED(result)) {
			return false;
		}

		indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
		indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
		indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
		indexBufferDesc.CPUAccessFlags = 0;
		indexBufferDesc.MiscFlags = 0;
		indexBufferDesc.StructureByteStride = 0;

		indexData.pSysMem = m_indices;
		indexData.SysMemPitch = 0;
		indexData.SysMemSlicePitch = 0;

		result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
		if (FAILED(result)) {
			return false;
		}

		delete[] vertices;
		vertices = NULL;

		return true;
}

void miv::Polygon::ShutdownBuffers() {
	if (m_indices) {
		m_indices = NULL;
	}

	if (m_vertices) {
		m_vertices = NULL;
	}

	if (m_indexBuffer) {
		m_indexBuffer->Release();
		m_indexBuffer = NULL;
	}

	if (m_vertexBuffer) {
		m_vertexBuffer->Release();
		m_vertexBuffer = NULL;
	}
}

void miv::Polygon::DrawBuffers(Graphics* graphics) {
	graphics->m_renderQueue->push(this);
}
